package juntos.lbcv.dao;

import java.util.List;

import juntos.lbcv.constant.TypeMarque;
import juntos.lbcv.model.RefBoiteVitesse;
import juntos.lbcv.model.RefCarburant;
import juntos.lbcv.model.RefDepartement;
import juntos.lbcv.model.RefLocalisation;
import juntos.lbcv.model.RefMarque;
import juntos.lbcv.model.RefModele;
import juntos.lbcv.model.RefRegion;
import juntos.lbcv.model.RefTypeAnnonceur;

import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;


@Repository("refDAO")
public class InstallRefDAO extends DAO  {

	public RefRegion persistRegion(String labelRegion) {
		String uKey = this.getUkey(labelRegion);
		if (uKey == null) return null;
		
		RefRegion region =  (RefRegion) findByUKey(RefRegion.class, uKey);
		if (region == null) {
			region = new RefRegion(uKey, labelRegion);
			this.persist(region);
		}
		return region;
	}
	
	
	public RefDepartement persistDepartement(RefRegion region, String labelDepartement) {
		String uKey = this.getUkey(labelDepartement);
		if (uKey == null) return null;
		
		RefDepartement departement =  (RefDepartement) findByUKey(RefDepartement.class, uKey);
		if (departement == null) {
			departement = new RefDepartement(region, uKey, labelDepartement);
			this.persist(departement);
		}
		return departement;
	}

	
	public RefLocalisation getLocalisationByLabelOrInsert(String cP, String label) {
		 String uKey = this.getUkey(cP+label);
			if (uKey == null) return null;
			
			RefLocalisation localisation =  (RefLocalisation) this.createCriteria(RefLocalisation.class)
					.add(Restrictions.eq("ukey", uKey)).uniqueResult();
			if (localisation == null) {
				
			}
			return localisation;
	}


	public RefMarque persistMarque(TypeMarque type, String labelMarque) {
		String uKey = this.getUkey(labelMarque);
		if (uKey == null) return null;
		
		RefMarque  marque =  (RefMarque) findByUKey(RefMarque.class, uKey);
		if (marque == null) {
			marque = new RefMarque(labelMarque, type.id, uKey);
			this.persist(marque);
		}
		return marque;
	}


	public RefModele persistModele(RefMarque marque, String labelModele) {
		String label = getLabelModel(marque.getLabel(),labelModele);
		String uKey = this.getUkey(label);
		if (uKey == null) return null;
		
		RefModele modele =  (RefModele) findByUKey(RefModele.class, uKey);
		if (modele == null) {
			modele = new RefModele(marque, label, uKey);
			this.persist(modele);
		}
		return modele;		
	}

	
}